home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Utilities / Programming / EnterAct 3.5 / hAWK project / AWK Source / GETOPT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1995-04-07  |  2.7 KB  |  109 lines  |  [TEXT/TOPC]

  1. /*
  2. **    @(#)getopt.c    2.5 (smail) 9/15/87
  3. */
  4.  
  5. /*
  6.  * Here's something you've all been waiting for:  the AT&T public domain
  7.  * source for getopt(3).  It is the code which was given out at the 1985
  8.  * UNIFORUM conference in Dallas.  I obtained it by electronic mail
  9.  * directly from AT&T.  The people there assure me that it is indeed
  10.  * in the public domain.
  11.  * 
  12.  * There is no manual page.  That is because the one they gave out at
  13.  * UNIFORUM was slightly different from the current System V Release 2
  14.  * manual page.  The difference apparently involved a note about the
  15.  * famous rules 5 and 6, recommending using white space between an option
  16.  * and its first argument, and not grouping options that have arguments.
  17.  * Getopt itself is currently lenient about both of these things White
  18.  * space is allowed, but not mandatory, and the last option in a group can
  19.  * have an argument.  That particular version of the man page evidently
  20.  * has no official existence, and my source at AT&T did not send a copy.
  21.  * The current SVR2 man page reflects the actual behavor of this getopt.
  22.  * However, I am not about to post a copy of anything licensed by AT&T.
  23.  */
  24. /* Slightly modified for THINK C 4 on the Mac by Ken Earle (Dynabyte) Aug 1991. */
  25.  
  26. #include <string.h>
  27. #include <unix.h>
  28.  
  29. /*#if defined(MSDOS) || defined(USG)*/
  30.  
  31. #define index strchr
  32.  
  33. /*#endif*/
  34.  
  35. /*LINTLIBRARY*/
  36. #define ERR(s, c)    if(opterr){\
  37.     char errbuf[2];\
  38.     errbuf[0] = c; errbuf[1] = '\n';\
  39.     (void) fwrite(argv[0], 1, (unsigned)strlen(argv[0]), stderr);\
  40.     (void) fwrite(s, 1, (unsigned)strlen(s), stderr);\
  41.     (void) fwrite(errbuf, 1, 2, stderr);}
  42.  
  43. /*extern char *index();*/
  44.  
  45. short    opterr = 1;
  46. short    optind = 1;
  47. short    optopt;
  48. char    *optarg;
  49.  
  50. short getopt(short argc, char **argv, char *opts);
  51. void InitGetOpt(void);
  52.  
  53. static short sp = 1;
  54.  
  55. short getopt(short argc, char **argv, char *opts)
  56. {
  57.     
  58.     register short c;
  59.     register char *cp;
  60.  
  61.     if(sp == 1)
  62.         if(optind >= argc ||
  63.            argv[optind][0] != '-' || argv[optind][1] == '\0')
  64.             return(EOF);
  65.         else if(strcmp(argv[optind], "--") == 0) {
  66.             optind++;
  67.             return(EOF);
  68.         }
  69.     optopt = c = argv[optind][sp];
  70.     
  71.     if(c == ':' || (cp=index(opts, c)) == NULL) {
  72.         ERR(": illegal option -- ", c);
  73.         if(argv[optind][++sp] == '\0') {
  74.             optind++;
  75.             sp = 1;
  76.         }
  77.         return('?');
  78.     }
  79.     /* colon in opts means preceding option takes an argument. */
  80.     if(*++cp == ':') {
  81.         if(argv[optind][sp+1] != '\0')
  82.             optarg = &argv[optind++][sp+1];
  83.         else if(++optind >= argc) {
  84.             ERR(": option requires an argument -- ", c);
  85.             sp = 1;
  86.             return('?');
  87.         } else
  88.             optarg = argv[optind++];
  89.         sp = 1;
  90.     } else {
  91.         if(argv[optind][++sp] == '\0') {
  92.             sp = 1;
  93.             optind++;
  94.         }
  95.         optarg = NULL;
  96.     }
  97.     return(c);
  98. }
  99.  
  100. void InitGetOpt()
  101.     {
  102.     
  103.     opterr = 1;
  104.     optind = 1;
  105.     optopt = 0;
  106.     optarg = NULL;
  107.     sp = 1;
  108.     }
  109.